﻿require([
"jquery",
"ko",
"bootstrap",
"jquery-slimscroll",
 "jquery-qrcode",
 "jquery-fancybox-thumbs",
"akcy"], function ($, ko) {
    var akcy = arguments[arguments.length - 1];
    akcy.ui.init();

    $.when(
          akcy.ajaxget("/ak56api/service/dictionary?codes=BranchesRoot,Nodes")
      ).done(function (dic) {
          var userForm = akcy.form().options({
              fields: [
                  { field: 'U_LoginName', value: ko.observable() },
                  { field: 'U_ID', value: ko.observable() },
                  { field: 'U_Name', value: ko.observable() },
                  { field: 'U_Gender', value: ko.observable() },
                  { field: 'U_Mobile', value: ko.observable() },
                  { field: 'U_Email', value: ko.observable() },
                  { field: 'U_Branch_ID', value: ko.observable() },
                  { field: 'U_Node_ID', value: ko.observable() },
                  { field: 'U_Position', value: ko.observable() },
                  { field: 'U_Phone', value: ko.observable() },
                  { field: 'U_Password', value: ko.observable() },
                  { field: 'U_QRCode', value: ko.observable() },
                  { field: 'U_Valid', value: ko.observable('1') },
                  { field: 'U_F_ID', value: ko.observable() }
              ],
              dataUpdateUrl: '/ak56api/users/UpdateUserNotCheckRights'
          }).optionsExtend({
              branches: ko.observableArray(dic["BranchesRoot"]),
              tempQRcode: ko.observable(""),
              nodes: ko.observableArray(dic["Nodes"]),
              updatepwd: ko.observable("0"),
              filePath: ko.observable(""),
              beforeUpdate: function (data) {
                  if (userForm.getInstance().updatepwd() == "1") {
                      data.U_Password = 'Y:' + data.U_Password();
                  }
              },
              updated: function () {
                  akcy.alert.success("保存成功！");
              },
              qrGeneration: function () {
                  var sessionid = akcy.session.get();
                  akcy.ajaxpost("/ak56api/QRCode/QRGenerationForReg?sessionId=" + sessionid).done(function (rst) {
                      userForm.getInstance().tempQRcode(rst);
                      $('#qrc').qrcode({
                          'text': rst
                      });
                  });
              },
              qrCancel: function () {
                  var sessionid = akcy.session.get();
                  akcy.ajaxpost("/ak56api/QRCode/QRUnRegiste?sessionId=" + sessionid).done(function (rst) {
                      userForm.getInstance().tempQRcode("");
                      userForm.getInstance().U_QRCode("");
                  });
              }
          }).bind("userInfo");

          $("#upload").click(function () {
              var uid = userForm.getInstance().U_ID();
              akcy.showupload(function (rst) {
                  userForm.getInstance().filePath(rst.url);
                  userForm.getInstance().U_F_ID(rst.fileid);
              }, { type: "UserPicture", businessid: uid });
          });

          userForm.load('/ak56api/users/GetUserBySession?session=' + akcy.session.get(), {}, function (rst) {
              if (userForm.getInstance().U_F_ID() != null) {
                  akcy.ajaxpost("/ak56api/service/FileInfo?fid=" + userForm.getInstance().U_F_ID()).done(function (rst) {
                      userForm.getInstance().filePath(rst[0].url);
                  });
              }
          });

      });
});